#!/usr/bin/python3
# author:szw
# 2023年06月27日
# Email:1259577135@qq.com
# desc:

import time
import requests
import json
import os

proxies={'http':'127.0.0.1:15732',
         'https':'127.0.0.1:15732'}
def get_page(key_word:str,after=None):

    headers = {
        "authority": "www.behance.net",
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "content-type": "application/json",
        "origin": "https://www.behance.net",
        "referer": "https://www.behance.net/search/images",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.41",
        "x-bcp": "d814ff56-c667-441a-8845-e4c1957fffe7",
        "x-requested-with": "XMLHttpRequest"
    }
    cookies = {
        "bcp": "d814ff56-c667-441a-8845-e4c1957fffe7",
    }
    url = "https://www.behance.net/v3/graphql"
    data = {
        "query": "\n      query GetImageSearchResults(\n        $query: query\n        $filter: SearchResultFilter\n        $first: Int!\n        $after: String\n        $similarStyleId: ModuleId\n        $shouldIncludeSimilarStyles: Boolean!\n      ) {\n        search(query: $query, type: IMAGE, filter: $filter, first: $first, after: $after) {\n          pageInfo {\n            hasNextPage\n            endCursor\n          }\n          nodes {\n            ... on ImageModule {\n              __typename\n              id\n              src\n              flexWidth\n              flexHeight\n              height\n              width\n              tags\n              colors {\n                r\n                g\n                b\n              }\n              nftData {\n                contractAddress\n                contractAddressUrl\n                tokenId\n                creatorName\n                creatorUrl\n                imageUrl\n                name\n                description\n              }\n              exifData {\n                makeAndModel {\n                  label\n                  value\n                }\n                lens {\n                  label\n                  value\n                }\n                focalLength {\n                  label\n                  value\n                  searchValue\n                }\n                iso {\n                  label\n                  value\n                }\n                exposureMode {\n                  label\n                  value\n                }\n                shutterSpeed {\n                  label\n                  value\n                  searchValue\n                }\n                aperture {\n                  label\n                  value\n                  searchValue\n                }\n              }\n              project {\n                id\n                slug\n                matureAccess\n                license {\n                  id\n                  label\n                  url\n                  text\n                  images\n                }\n                url\n                name\n                slug\n                isOwner\n                owners {\n                  ...OwnerFields\n                  adobeId\n                  images {\n                    size_50 {\n                      url\n                    }\n                  }\n                }\n                stats {\n                  appreciations {\n                    all\n                  }\n                  views {\n                    all\n                  }\n                  comments {\n                    all\n                  }\n                }\n                tools {\n                  id\n                  title\n                  url\n                  synonym {\n                    iconUrl\n                    downloadUrl\n                    galleryUrl\n                    title\n                  }\n                }\n                fields {\n                  id\n                  label\n                  slug\n                  url\n                }\n                covers {\n                  size_115 {\n                    url\n                  }\n                  size_404 {\n                    url\n                  }\n                }\n              }\n              imageSizes {\n                size_disp {\n                  url\n                  width\n                  height\n                }\n                size_max_632 {\n                  url\n                  width\n                  height\n                }\n                size_max_316 {\n                  url\n                  width\n                  height\n                }\n                size_max_158 {\n                  url\n                  width\n                  height\n                }\n                size_hd {\n                  url\n                  width\n                  height\n                }\n                size_fs {\n                  url\n                  width\n                  height\n                }\n                size_2800_opt_1 {\n                  url\n                  width\n                  height\n                }\n                size_1400_opt_1 {\n                  url\n                  width\n                  height\n                }\n                size_max_1200 {\n                  url\n                  width\n                  height\n                }\n                size_disp_still {\n                  url\n                  width\n                  height\n                }\n                size_2800_still {\n                  url\n                  width\n                  height\n                }\n                size_1400_still {\n                  url\n                  width\n                  height\n                }\n                size_632_still {\n                  url\n                  width\n                  height\n                }\n                size_316_still {\n                  url\n                  width\n                  height\n                }\n                size_158_still {\n                  url\n                  width\n                  height\n                }\n              }\n            }\n          }\n          metaContent {\n            toolCard {\n              cta {\n                text\n                url\n              }\n              description\n              links {\n                text\n                url\n                type\n              }\n              slug\n              title\n            }\n            csam {\n              isCSAMViolation\n              description\n              helpResource\n              reportingOption\n            }\n            followableTag {\n              isFollowing\n              tag {\n                id\n                title\n              }\n            }\n          }\n        }\n\n        imageModule(id: $similarStyleId) @include(if: $shouldIncludeSimilarStyles) {\n          src\n          width\n          height\n          nftData {\n            contractAddress\n            contractAddressUrl\n            tokenId\n            creatorName\n            creatorUrl\n            imageUrl\n            name\n            description\n            isInUserNftProject\n          }\n          project {\n            url\n            owners {\n              ...OwnerFields\n              adobeId\n              images {\n                size_50 {\n                  url\n                }\n              }\n            }\n          }\n          imageSizes {\n            size_max_632 {\n              url\n              width\n              height\n            }\n            size_max_316 {\n              url\n              width\n              height\n            }\n            size_max_158 {\n              url\n              width\n              height\n            }\n          }\n        }\n      }\n      \n  fragment OwnerFields on User {\n    displayName\n    hasPremiumAccess\n    id\n    isFollowing\n    isProfileOwner\n    location\n    locationUrl\n    url\n    username\n    availabilityInfo {\n      availabilityTimeline\n      isAvailableFullTime\n      isAvailableFreelance\n    }\n  }\n\n    ",
        "variables": {
            "query": key_word,
            "filter": {},
            "first": 48,
            # "after": "MjQ1",
            "similarStyleId": 0,
            "shouldIncludeSimilarStyles": False
        }
    }
    if after:
        data["variables"]["after"] = after

    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url,proxies=proxies, headers=headers, cookies=cookies, data=data)
    requests.session().close()
    requests.session().keep_alive = False
    if response.text:
        return response.json()

def svae_image(url,PWD = 'behance/concept/'):
    name = url.split('/')[-1]
    # 将图片以最后的数字+后缀命名，方便检查文件是否存在
    filename = PWD + name
    if os.path.isfile(filename):  # 如果文件已爬取，则不再重复爬取
        print("文件存在：", filename)
        return
    # urllib.request.urlretrieve(url, filename=path+filename)
    try:
        response = requests.get(url, proxies=proxies, stream=True, timeout=30)
        with open(filename, 'wb') as fd:
            fd.write(response.content)
        requests.session().close()
    except  Exception as e:
        print(e)




def down_image(file, url):      # 下载图片方法
    print("开始下载：", url)
    for i in range(5):
        try:
            response = requests.get(url,proxies=proxies,timeout=10)
            with open(file, 'wb') as fd:
                fd.write(response.content)
            requests.session().close()
            return 1
        except Exception as e:
            time.sleep(5)
    print("下载失败了",url)
    return 0


def save_summary(path, file, jsondata):
    with open(path + file, 'w') as f:
        f.write(json.dumps(jsondata))
    f.close()


def save_txt(PWD,txt_filename, data):
    with open(PWD + txt_filename, 'w') as f:
        f.write(data)
    f.close()



def main():
    keyword = 'futuristic car'
    try:
        after = 'ODkxOA==' # page: 181 after_code: Mzc3Mw==
        response = get_page(keyword,after)
        after = response['data']['search']['pageInfo']['endCursor']
        page = 181
        print('page:',page,'after_code:',after)
        nodes = response['data']['search']['nodes']
        for node in nodes:
            # pprint(node)
            print(node['id'], str(node['src']).replace('disp', 'max_632'))
            url = str(node['src']).replace('disp', 'max_632')
            svae_image(url,PWD = 'behance/future/')

        while after and page <= 1000:
            time.sleep(10)
            response = get_page(keyword,after=after)
            # pprint(response)
            after =response['data']['search']['pageInfo']['endCursor']
            page+=1
            print('page:',page,'after_code:',after)
            nodes = response['data']['search']['nodes']
            for node in nodes:
                # pprint(node)
                print(node['id'],str(node['src']).replace('disp','max_632'))
                url = str(node['src']).replace('disp','max_632')
                svae_image(url,PWD = 'behance/future/')
    except Exception as  e:
        print(e)
if __name__ == '__main__':
    from pprint import pprint
    main()

    pass